{% extends "base.html" %}

{% block title %}{{ card.key }}{% endblock title %}

{% import "card_macros.html" as card_macros %}

{% macro blank_or_date(field) %}
    {% if field %}
        {{ field.strftime("%m/%d/%Y") }}
    {% else %}

    {% endif %}
{% endmacro %}

{% macro blank_or_datetime(field) %}
    {% if field %}
        {{ field.strftime("%m/%d/%Y %I:%M %p") }}
    {% else %}

    {% endif %}
{% endmacro %}

{% macro blank_or_value(field) %}
    {% if field %}
        {{ field.strftime("%m/%d/%Y %I:%M %p") }}
    {% else %}

    {% endif %}
{% endmacro %}

{% macro card_controls(request, card) %}
      <p class="card_controls">
           {% set next_url = request.path %}
           <a href="{{ url_for('team', team_slug=card.team|slugify) }}#card_{{ card.key }}">{{ card.team }}</a> /
           <a href="{{ card.ticket_system.get_ticket_url() }}">JIRA</a> /
            <a href="{{ url_for('card_edit', key=card.key, next=next_url) }}">Edit</a> / <a href="{{ url_for('card_delete', key=card.key, next=next_url) }}">Delete</a> /
            {% if not card.done_date %}
                <a href="{{ url_for('card_block', key=card.key, next=next_url) }}">{% if card.blocked %}Unblock{% else %}Block{% endif %}</a> /
            {% endif %}
<a href="{{ url_for('state') }}">Home</a>

      </p>
{% endmacro %}

{% block content %}

<div class="metric card_detail" id="content_detail">
      <h2 style="font-weight: bold;">
        {{ card.key }} /
            {% if card.done_date %}
                {{ card.cycle_time }}
            {% else %}
                {{ card.current_cycle_time() }}
            {% endif %} days
      </h2>
      <div class="content">

      {{ card_controls(request, card) }}

      <h3>{{ card.ticket_system_data.get('summary', card.title) }}</h2>

        <table>
            <tr>
                <th>Cycle time</th>
                <th>Lead time</th>
                <th>Blocked</th>
                <th>Backlogged</th>
                <th>Started</th>
                <th>Done</th>
                <th>Due</th>
            </tr>
            <tr>
                <td>
                    {% if card.done_date %}
                        {{ card.cycle_time }}
                    {% else %}
                        {{ card.current_cycle_time() }}
                    {% endif %}
                </td>
                <td>
                    {% if card.done_date %}
                        {{ card.lead_time }}
                    {% else %}
                        {{ card.current_lead_time() }}
                    {% endif %}
                </td>
                <td>
                    {{ card_macros.hours_to_days(card.blocked_time) }}
                <td>
                    {{ blank_or_date(card.backlog_date) }}
                </td>
                <td>
                    {{ blank_or_date(card.start_date) }}
                </td>
                <td>
                    {{ blank_or_date(card.done_date) }}
                </td>
                <td>
                    {{ blank_or_date(card.due_date ) }}
                </td>
            </tr>
        </table>


        <table id="card-state-history">
            <caption>History</caption>
            <tr>
                <th>State</th>
                <th>Duration</th>
                <th>Entered</th>
                <th>Exited</th>
                <th>Service Class</th>
            </tr>
            {% for log in card_log %}
            <tr>
                <td>{{ log.state }}</td>
                <td>{{ card_macros.hours_to_days(log.duration) }}</td>
                <td>{{ blank_or_datetime(log.entered) }}</td>
                <td>{{ blank_or_datetime(log.exited) }}</td>
                <td>{{ log.service_class }}</td>
            </tr>
            {% endfor %}
        </table>


        {% if card.blocked_ever %}
        <table>
            <caption>Blockers</caption>
            <tr>
                <th>Start</th>
                <th>End</th>
                <th>Reason</th>
            </tr>

            {% for blocker in card.blockers %}
            <tr>
                <td>{{ blank_or_date(blocker.blocked_at) }}</td>
                <td>{{ blank_or_date(blocker.unblocked_at) }}</td>
                <td>{{ blocker.reason }}</td>
            </tr>
            {% endfor %}
        </table>
        {% endif %}


      {% if card.ticket_system_data %}
      <h4>JIRA information</h4>
      <ul>

        {% set assignee = card.ticket_system_data.get('assignee', '') %}
        {% set reporter = card.ticket_system_data.get('reporter', '') %}
        <li>Last synced: {{ card._ticket_system_updated_at|timesince }}</li>

        {% set jira_update = card.ticket_system_data.get('updated', None) %}
        <li>Last updated in JIRA: {% if jira_update %}{{ jira_update|timesince }}{% else %}Unknown{% endif %}</li>
        <li>Assigned: {% if assignee %}<a href="{{ url_for('person', name=assignee) }}">{{ assignee }}</a>{% endif %}</li>
        <li>Reporter: {% if reporter %}<a href="{{ url_for('person', name=reporter) }}">{{ reporter }}</a>{% endif %}</li>
        <li>Developers:
                <ul>
                {% for dev in card.ticket_system_data.get('developers', []) %}
                <li><a href="{{ url_for('person', name=dev) }}">{{ dev }}</a></li>
                {% endfor %}
                </ul>
        </li>
        <li>Testers:
                <ul>
                {% for qa in card.ticket_system_data.get('qaers', []) %}
                <li><a href="{{ url_for('person', name=qa) }}">{{ qa }}</a></li>
                {% endfor %}
                </ul>
        </li>
        <li>
                {% set icon = card.ticket_system_data.get('status', {}).get('icon') %}
                {% set status_label = card.ticket_system_data.get('status', {}).get('name', '') %}
                {% if icon %}
                <img src="{{ icon }}" alt="{{ status_label }}" title="{{ status_label }}" width="16" height="16" />
                {% endif %}
                {{ status_label }}
        </li>
        <li>    {% set icon = card.ticket_system_data.get('type', {}).get('icon') %}
                {% set status_label = card.ticket_system_data.get('type', {}).get('name', '') %}
                {% if icon %}
                <img src="{{ icon }}" alt="{{ status_label }}" title="{{ status_label }}" width="16" height="16" />
                {% endif %}
                {{ status_label }}
        </li>
        </ul>

      {% endif %}

      {{ card_controls(request, card) }}


      </div>

</div>

{% endblock content %}
